using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;

namespace IllSoft.Xvcc.Volano.Client
{
    class ConnectionManager
    {
        private TcpClient _tcpClient;

        #region (De-)Constructor(s)

        public ConnectionManager(ConnectionArgs connectionArgs)
        {
            this.ConnectionArgs = connectionArgs;
        }

        #endregion

        #region Properties

        public BufferedStream BufferedStream { get; private set; }
        public ConnectionArgs ConnectionArgs { get; private set; }
        public VolanoReader VolanoReader { get; private set; }
        public VolanoWriter VolanoWriter { get; private set; }

        #endregion

        #region Methods
        
        public void BeginConnect(AsyncCallback requestCallback)
        {
            this._tcpClient = new TcpClient();

            this._tcpClient.BeginConnect(
                this.ConnectionArgs.Target.Address,
                this.ConnectionArgs.Target.Port,
                requestCallback,
                this);
        }

        public void EndConnect(IAsyncResult result)
        {
            this._tcpClient.EndConnect(result);

            this.BufferedStream = new BufferedStream(this._tcpClient.GetStream());
            this.VolanoReader = new VolanoReader(this.BufferedStream);
            this.VolanoWriter = new VolanoWriter(this.BufferedStream);
        }

        public void Disconnect(Action x)
        {
            //TODO: Implement/Test me
            this.BufferedStream.Close();
            this._tcpClient.Close();
        }

        #endregion
    }
}